<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2176 – `time` is undefined for pipelines. time single stage or `bash -c` instead.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2176 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2176">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="time-is-undefined-for-pipelines-time-single-stage-or-bash--c-instead"><code>time</code>
is undefined for pipelines. time single stage or <code>bash -c</code>
instead.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2176.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">time</span> foo <span class="kw">|</span> <span class="ex">bar</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<p>To time the most relevant stage:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2176.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">foo</span> <span class="kw">|</span> <span class="kw">{</span> <span class="bu">time</span> bar<span class="kw">;</span> <span class="kw">}</span></span></code></pre></div>
<p>To time everything in a pipeline:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2176.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="bu">time</span> bash <span class="at">-c</span> <span class="st">&#39;foo | bar&#39;</span></span></code></pre></div>
<p>Note that you can not use <code>time sh -c</code> to time an entire
pipeline, because POSIX does not guarantee that anything other than the
last stage is waited upon by the shell.</p>
<h3 id="rationale">Rationale:</h3>
<p>This behavior is explicitly left undefined <a
href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/time.html">in
POSIX</a>.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>None. This warning is not emitted in <code>ksh</code> or
<code>bash</code> where <code>time</code> is defined for pipelines.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


